<?php
/**
 * Eka Framework 0.3.0
 *
 * @copyright Copyright (c) 2008 Alexey Yu. Chuprina <656b61@gmail.com> (http://php-eka.googlecode.com)
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 *
 * @version $Id$
 */

require_once 'Eka/Db/Table/Abstract.php';
require_once 'Eka/Autoloader.php';

/**
 * Eka_Db_Table
 *
 * @package Db
 * @subpackage Table
 */
class Eka_Db_Table extends Eka_Db_Table_Abstract
{
	/**
	 * Classname for row
	 *
	 * @var string
	 */
	protected $rowClass = 'Eka_Db_Table_Row';
	
	/**
	 * Construct
	 *
	 * @param Eka_Db_Adapter_Abstract $adapter
	 * @param string $tableName
	 * @param string $tableSchema
	 */
	public function __construct(Eka_Db_Adapter_Abstract $adapter, $tableName, $tableSchema = null)
	{
		parent::__construct($adapter, $tableName, $tableSchema);

		$metadata = $this->adapter->describeTable($this->tableName, $this->tableSchema);

		$this->columns = array();
		$this->columnNames = array();
		$this->primary = array();
		foreach ($metadata['columns'] as $column) {
			if ($column['primary']) {
				$this->primary[$column['primary_position']] = $column['name'];
			}
			$this->columnNames[] = $column['name'];
			$this->columns[$column['name']] = array(
				'null' => $column['null'],
				//'type' => $column['type'],
				'default' => $column['default']
			);
		}
		if (count($this->primary) == 0) {
			$this->primary = null;
		}
		
		Eka_Autoloader::load($this->rowClass);
	}
	
	/**
	 * @param array $data
	 * @param bool $new
	 * @return Eka_Db_Table_Row
	 */
	protected function getRow(array $data = null, $new = false)
	{
		return new $this->rowClass($this, $data, $new);
	}
}